<?php
require_once 'system/core/Model.php';
class Access_models extends CI_Model{
  function __construct(){
      parent::__construct();
      $this->db = $this->load->database('odbc://:ithITtECH@access_ini/HITFPTA', true);
//      $this->db = $this->load->database('odbc://:ithITtECH@Driver={Microsoft Access Driver (*.mdb)}/D:HITFPTA.mdb', true);
  }
  function insert($table, $kirim){
    $this->db->insert($table, $kirim);
//    return $this->db->insert_id();
  }
  function insert_batch($table, $kirim){
    $this->db->insert_batch($table, $kirim);
    return $this->db->insert_id();
  }
  function update_duplicate($table, $kirim, $else){
    $no = 0;
    foreach($kirim as $key => $krm){
      if($no > 0){
        $field .= ", ";
        $value .= ", ";
      }
      $field .= "`$key`";
      if($krm > 0 OR $krm)
        $value .= "'$krm'";
      else
        $value .= "NULL";
      $no++;
    }
    $no = 0;
    foreach($else as $ky => $el){
      $le= "";
      if($el > 0 OR $el)
        $le .= "'$el'";
      else
        $le .= "NULL";
      if($no > 0)
        $update .= ",";
      $update .= "`$ky` = $le";
      $no++;
    }
    $query = "
      INSERT INTO {$table} ({$field}) VALUES({$value}) ON DUPLICATE KEY UPDATE {$update};
      ";
//      print $query;die;
    $this->db->query($query);
    return $this->db->insert_id();
  }
  function delete($table, $where){
    return $this->db->delete($table, $where); 
  }
  function update($table, $where, $kirim){
    $this->db->where($where);
    return $this->db->update($table, $kirim);
  }
  function get_field($table, $field, $where = array()){
    $this->db->select($field." as a");
    if($where)
      $this->db->where($where);
    $w = $this->db->get($table)->row();
    if($w)
      return $w->a;
    else
      return false;
  }
  function get_dropdown($table, $key, $name, $pilih = TRUE, $where = array(), $field_not = "nbs", $not_in = array(), $tambahan = ""){
    if($pilih === TRUE)
      $data[0] = "- Pilih -";
    $rec = $this->get($table, $where, $field_not, $not_in);
    if(is_array($rec)){
      foreach ($rec as $value) {
        $data[$value->$key] = $value->$name;
        if($tambahan)
          $data[$value->$key] .= " - ".$value->$tambahan;
      }
    }
    return $data;
  }
  function get_query($query){
    return $this->db->query($query)->result();
  }
  function get($table, $where = array(), $field_not = "nbs", $not_in = array(), $select = "*"){
    if($field_not != "nbs" AND $not_in)
      $this->db->where_not_in($field_not, $not_in);
    if($where)
      $this->db->where($where);
    $this->db->select($select);
    $data = $this->db->get($table)->result();
    if(is_array($data))
      return $data;
    else
      return array();
  }
  function get_array($field, $table, $where = array(), $field_not = "nbs", $not_in = array()){
    $temp = $this->get($table, $where, $field_not, $not_in);
    $return = array();
    if(is_array($temp)){
      foreach ($temp as $key => $value) {
        $return[] = $value->$field;
      }
    }
    return $return;
  }
  function get_user_outlet($id_outlet, $privilege){
    $query = "
      SELECT A.*
      FROM m_users AS A
      JOIN d_user_privilege AS B ON A.id_users = B.id_users
      JOIN m_privilege AS C ON B.id_privilege = C.id_privilege
      JOIN d_outlet_users AS D ON A.id_users = D.id_users AND D.id_outlet = {$id_outlet}
      WHERE C.name = '{$privilege}'
      ";
    return $this->get_query($query);
  }
  function get_outlet_user($id_users){
    $query = "
      SELECT A.*
      FROM m_outlet AS A
      JOIN d_outlet_users AS B ON A.id_outlet = B.id_outlet AND B.id_users = {$id_users}
      ";
    return $this->get_query($query);
  }
  function truncate($table = ''){
    return $this->db->truncate($table);
  }
  function ambildah(){
    return get_class_methods('Users');
  }
  function query($query){
    return $this->db->query($query);
  }
  function nicename($string){
    return strtolower(str_replace(" ", "_", str_replace("-", "_", $string)));
  }
}
?>
